package cn.icanci.loopstack.bic.biz.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.google.common.collect.Lists;

import cn.icanci.loopstack.bic.biz.mapper.UseCaseMapper;
import cn.icanci.loopstack.bic.biz.service.UseCaseService;
import cn.icanci.loopstack.bic.common.model.TextValue;
import cn.icanci.loopstack.bic.common.model.base.UseCaseVO;
import cn.icanci.loopstack.bic.dal.mongo.common.PageList;
import cn.icanci.loopstack.bic.dal.mongo.daointerface.UseCaseDAO;
import cn.icanci.loopstack.bic.dal.mongo.dateobject.UseCaseDO;

/**
 * @author icanci
 * @since 1.0 Created in 2023/09/10 20:10
 */
@Service
public class UseCaseServiceImpl implements UseCaseService {

    @Resource
    private UseCaseDAO    useCaseDAO;
    @Resource
    private UseCaseMapper useCaseMapper;

    @Override
    public List<UseCaseVO> queryAll() {
        return useCaseMapper.dos2vos(useCaseDAO.queryAll());
    }

    @Override
    public void save(UseCaseVO useCaseVO) {
        // TODO 日志记录
        if (doInsert(useCaseVO)) {
            UseCaseDO insert = useCaseMapper.vo2do(useCaseVO);
            useCaseDAO.insert(insert);
        } else {
            useCaseDAO.update(useCaseMapper.vo2do(useCaseVO));
        }
    }

    @Override
    public UseCaseVO queryById(String id) {
        return useCaseMapper.do2vo(useCaseDAO.queryOneById(id));
    }

    @Override
    public PageList<UseCaseVO> queryPage(UseCaseVO useCaseVO, int pageNum, int pageSize) {
        PageList<UseCaseDO> pageQuery = useCaseDAO.pageQuery(useCaseMapper.vo2do(useCaseVO), pageNum, pageSize);
        return new PageList<>(useCaseMapper.dos2vos(pageQuery.getData()), pageQuery.getPaginator());
    }

    @Override
    public UseCaseVO queryBySystemUuidAndName(String systemUuid, String name) {
        return useCaseMapper.do2vo(useCaseDAO.queryBySystemUuidAndName(systemUuid, name));
    }

    @Override
    public UseCaseVO queryBySystemUuidAndKey(String systemUuid, String key) {
        return useCaseMapper.do2vo(useCaseDAO.queryBySystemUuidAndKey(systemUuid, key));
    }

    @Override
    @SuppressWarnings("all")
    public List<TextValue> loadSelector(String systemUuid) {
        List<UseCaseVO> useCases = queryAll();
        List<TextValue> textValues = Lists.newArrayList();
        for (UseCaseVO useCaseVO : useCases) {

            if (!StringUtils.equals(systemUuid, useCaseVO.getSystemUuid())) {
                continue;
            }

            String label;
            if (isDeleted(useCaseVO)) {
                label = String.format(DELETED_FORMAT, useCaseVO.getUseCaseName());
            } else {
                label = String.format(NOT_DELETED_FORMAT, useCaseVO.getUseCaseName());
            }
            String value = useCaseVO.getUuid();
            textValues.add(new TextValue(label, value));
        }
        return textValues;
    }

    @Override
    public List<TextValue> loadAllSelector() {
        List<UseCaseVO> useCases = queryAll();
        List<TextValue> textValues = Lists.newArrayList();
        for (UseCaseVO useCaseVO : useCases) {
            String label;
            if (isDeleted(useCaseVO)) {
                label = String.format(DELETED_FORMAT, useCaseVO.getUseCaseName());
            } else {
                label = String.format(NOT_DELETED_FORMAT, useCaseVO.getUseCaseName());
            }
            String value = useCaseVO.getUuid();
            textValues.add(new TextValue(label, value));
        }
        return textValues;
    }

    @Override
    public UseCaseVO queryBySystemAndUseCase(String systemUuid, String useCaseUuid) {
        return useCaseMapper.do2vo(useCaseDAO.queryBySystemAndUseCase(systemUuid, useCaseUuid));
    }
}
